package com.hsgene.common.util.support;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 花生仁分页支持
 *
 * @author wangbing
 * @version 1.0, 2018/7/18
 */
public class PeanutPagination<T> implements Serializable {

    private static final long serialVersionUID = 3505362118495881042L;

    /**
     * 总共数量
     */
    private int count;

    /**
     * 当前页数
     */
    private int pageNo;

    /**
     * 分页大小
     */
    private int pageSize;

    /**
     * 总页数
     */
    private int totalPage;

    /**
     * 是否是最后一页
     */
    private boolean lastPage;

    /**
     * 分页查询结果
     */
    private List<T> list = new ArrayList<>();

    public PeanutPagination() {
    }

    private PeanutPagination(int totalCount, int pageNo, int pageSize, List<T> list) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        this.lastPage = this.pageNo >= this.totalPage;
        this.list = list;
    }

    private PeanutPagination(int totalCount, LimitHelper limiter, List<T> list) {
        this(totalCount, limiter.getPage(), limiter.getSize(), list);
    }

    /**
     * 根据分页参数和数据总量创建一个PeanutPagination实例.
     *
     * @param limitHelper 分页参数组件
     * @param totalCount  数据总量
     * @return PeanutPagination实例
     */
    public static <T> PeanutPagination<T> newInstance(LimitHelper limitHelper, int totalCount) {
        return newInstance(limitHelper, totalCount, null);
    }

    /**
     * 根据分页参数和数据总量创建一个PeanutPagination实例.
     *
     * @param limitHelper 分页参数组件
     * @param totalCount  数据总量
     * @param list        分页结果集
     * @return PeanutPagination实例
     */
    public static <T> PeanutPagination<T> newInstance(LimitHelper limitHelper, int totalCount, List<T> list) {
        return new PeanutPagination<>(totalCount, limitHelper, list);
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public boolean isLastPage() {
        return lastPage;
    }

    public void setLastPage(boolean lastPage) {
        this.lastPage = lastPage;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
